﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;

namespace ArmoryViewerMobile
{
    class Settings
    {
        private IniReader settings;

        //Settings variables
        private string armoryUrl;
        private string wowheadLocal;
        private bool hideWhenMinimized;
        private bool connectOnStartup;
        private bool downloadSockets;
        private bool handleAvProtocol;
        private int searchType;

        public string ArmoryUrl
        {
            get { return armoryUrl; }
            set { armoryUrl = value; }
        }

        public bool ShowWarning { get; set; }

        public bool HideWhenMinimized
        {
            get { return hideWhenMinimized; }
            set { hideWhenMinimized = value; }
        }

        public bool ConnectOnStartup
        {
            get { return connectOnStartup; }
            set { connectOnStartup = value; }
        }

        public bool DownloadSockets
        {
            get { return downloadSockets; }
            set { downloadSockets = value; }
        }

        public string WowheadLocalization
        {
            get { return wowheadLocal; }
            set { wowheadLocal = value; }
        }

        public int SearchType
        {
            get { return searchType; }
            set { searchType = value; }
        }

        public bool HandleAvProtocol
        {
            get { return handleAvProtocol; }
            set
            {
                if (handleAvProtocol != value)
                {
                    handleAvProtocol = value;
                    RegistryKey registry = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes");
                    if (value)
                    {
                        try
                        {
                            RegistryKey av = registry.CreateSubKey("av");
                            av.SetValue("", "URL:Armory Viewer Protocol");
                            av.SetValue("URL Protocol", "", RegistryValueKind.String);
                            av.CreateSubKey("DefaultIcon").SetValue("", Program.CurrentDirectory + "Armory Viewer.exe");
                            av.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue("", Program.CurrentDirectory + "Armory Viewer.exe \"%1\"");
                        }
                        catch (Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message, "Could not access registry", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Hand, System.Windows.Forms.MessageBoxDefaultButton.Button1);
                        }
                    }
                    else
                    {
                        registry.DeleteSubKeyTree("av");
                    }
                }
            }
        }

        public Settings(String iniFile)
        {
            settings = new IniReader(iniFile);
        }

        public Settings()
        {
            settings = new IniReader(Program.CurrentDirectory + "/settings.ini");
        }

        public void Load()
        {
            armoryUrl = settings.ReadString("ArmoryViewer", "armoryUrl", "http://www.wowarmory.com");
            connectOnStartup = settings.ReadBoolean("ArmoryViewer", "connectOnStartup", false);
            hideWhenMinimized = settings.ReadBoolean("ArmoryViewer", "hideWhenMinimized", true);
            downloadSockets = settings.ReadBoolean("ArmoryViewer", "downloadSockets", true);
            wowheadLocal = settings.ReadString("ArmoryViewer", "wowheadLocal", "www");
            searchType = settings.ReadInteger("ArmoryViewer", "searchType", 1);
            handleAvProtocol = settings.ReadBoolean("ArmoryViewer", "handleAvProtocol", (Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Classes").OpenSubKey("av") != null) ? true : false);
            ShowWarning = settings.ReadBoolean("ArmoryViewer", "showWarning", true);
        }

        public void Save()
        {
            settings.Write("ArmoryViewer", "armoryUrl", armoryUrl);
            settings.Write("ArmoryViewer", "connectOnStartup", connectOnStartup);
            settings.Write("ArmoryViewer", "hideWhenMinimized", hideWhenMinimized);
            settings.Write("ArmoryViewer", "downloadSockets", downloadSockets);
            settings.Write("ArmoryViewer", "wowheadLocal", wowheadLocal);
            settings.Write("ArmoryViewer", "searchType", searchType);
            settings.Write("ArmoryViewer", "handleAvProtocol", handleAvProtocol);
            settings.Write("ArmoryViewer", "showWarning", ShowWarning);
        }
    }
}
